package com.pureut.storage.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.PickingCompleteInfoDto;
import com.pureut.storage.export.PickingCompleteExport;
import com.pureut.storage.service.PickingCompleteService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.PickingCompleteVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.UpdateDocVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 返工出库控制层
 * @author: CM
 * @date: 2022/12/29  15:50
 */

@RestController
@RequestMapping("/pickingComplete")
public class PickingCompleteController extends BaseController {

    @Resource
    PickingCompleteService pickingCompleteService;

    /**
     * 新增返工出库单
     */
    @RequiresPermissions("warehouse:outbound:reworksto:add")
    @Log(title = "新增返工出库单", businessType = BusinessType.INSERT)
    @PostMapping("/addPickingComplete")
    public AjaxResult addPickingComplete(@RequestBody PickingCompleteVo pickingComPleteVo) throws Exception {
        return toAjax(pickingCompleteService.addPickingComplete(pickingComPleteVo));
    }

    /**
     * 关结返工出库单
     */
    @Log(title = "关结返工出库单", businessType = BusinessType.UPDATE)
    @RequiresPermissions("warehouse:outbound:reworksto:close")
    @PostMapping("/closeComplete")
    public AjaxResult closeComplete(@RequestBody PickingCompleteVo pickingComPleteVo) throws Exception {
        return toAjax(pickingCompleteService.closeComplete(pickingComPleteVo));
    }

    /**
     * 修改返工出库单
     */
    @RequiresPermissions("warehouse:outbound:reworksto:edit")
    @Log(title = "修改返工出库", businessType = BusinessType.UPDATE)
    @PostMapping("/updateComplete")
    public AjaxResult updateComplete(@RequestBody PickingCompleteVo pickingComPleteVo) throws Exception {
        return toAjax(pickingCompleteService.updateComplete(pickingComPleteVo));
    }

    /**
     * 删除单据
     */
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @RequiresPermissions("warehouse:outbound:reworksto:remove")
    @DeleteMapping("/deleteComplete/{ids}")
    public AjaxResult deleteOutComplete(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingCompleteService.deleteOutComplete(ids));
    }

    /**
     * 查看单据
     */
    @RequiresPermissions("warehouse:outbound:reworksto:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingCompleteService.view(id));
    }

    /**
     *  反攻出库列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(PickingCompleteVo pickingComPleteVo) {
        startPage();
        //查询返工出库列表
        List<PickingCompleteInfoDto> list = pickingCompleteService.getList(pickingComPleteVo);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    @RequiresPermissions("warehouse:outbound:reworksto:export")
    public AjaxResult inspectionMessageExport(HttpServletResponse response,PickingCompleteVo pickingComPleteVo) throws IOException {
        List<PickingCompleteExport> infoExport = pickingCompleteService.getInfoExport(pickingComPleteVo);
        ExcelUtil<PickingCompleteExport> excelUtil = new ExcelUtil<PickingCompleteExport>(PickingCompleteExport.class);
        excelUtil.excelWriter(response, infoExport, "返工出库信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 返工出库Pda
     */
    @RequiresPermissions("warehouse:outbound:reworksto:outbound")
    @Log(title = "返工出库Pda", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public AjaxResult out(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingCompleteService.out(pickingRestDbVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    public AjaxResult finishProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingCompleteService.finishProduction(id));
    }


    /**
     * 出库交接
     */
    @RequiresPermissions("warehouse:outbound:reworksto:handover")
    @Log(title = "返工出库交接", businessType = BusinessType.INSERT)
    @PostMapping("/overProduction")
    public AjaxResult overProduction(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingCompleteService.overProduction(pickingRestDbVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/competeUpdateStatus")
    public AjaxResult competeUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(pickingCompleteService.competeUpdateStatus(updateDocVo));
    }

}
